from typing import *

# ==================
#   宏赋值命令转换器
# ==================

# key 是 宏的名字
# 参数是 变量名 和 宏
macro_move_command_translators: Dict[str, Callable[[str, str, List[str]], str]] = {}


def macro_move_command_translator(name):
    def wrapper(func):
        macro_move_command_translators[name] = func
        return func

    return wrapper


# Sensor

@macro_move_command_translator('read')
def mmct_read(var: str, obj: str, args: List[str]) -> str:
    return f'read {var} {obj} {args[0]}'


@macro_move_command_translator('sensor')
def mmct_sensor(var: str, obj: str, args: List[str]) -> str:
    # sensor res block @enabled
    return f'sensor {var} {obj} {args[0]}'


@macro_move_command_translator('lookup')
def mmct_lookup(var: str, obj: str, args: List[str]) -> str:
    return f'lookup {args[0]} {var} {args[1]}'


@macro_move_command_translator('link')
def mmct_link(var: str, obj: str, args: List[str]) -> str:
    return f'getlink {var} {args[0]}'


@macro_move_command_translator('radar')
def mmct_radar(var: str, obj: str, args: List[str]) -> str:
    args = args.copy()
    if len(args) == 3:
        args.insert(1, 'any')
        args.insert(1, 'any')
    elif len(args) == 4:
        args.insert(1, 'any')

    return f'radar {args[0]} {args[1]} {args[2]} {args[4]} {obj} {args[3]} {var}'


@macro_move_command_translator('$within')
def mmct_ucontrol_within(var: str, obj: str, args: List[str]) -> str:
    return f'ucontrol within {args[0]} {args[1]} {args[2]} {var}'


@macro_move_command_translator('$radar')
def mmct_ucontrol_radar(var: str, obj: str, args: List[str]) -> str:
    if len(args) <= 3:
        args.insert(1, 'any')
    if len(args) <= 4:
        args.insert(2, 'any')
    return f'uradar {args[0]} {args[1]} {args[2]} {args[4]} 0 {args[3]} {var}'
